<?php


namespace App\Http\Controllers\Mini\Shop;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Wechat\PayController;
use App\Http\Requests\Mini\Shop\orderPay;
use App\Jobs\SettlementShippingJob;
use App\Jobs\ShopIntegralJob;
use App\Jobs\ShopNoticeJob;
use App\Services\OrderService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 用户订单
 * Class OrderController
 * @package App\Http\Controllers\Mini
 */
class OrderController extends Controller
{
    public $orderModel;
    public $pay_wechat;

    public function __construct()
    {
        $this->orderModel = new OrderService();
        $this->pay_wechat = new PayController();
    }


    /**
     * 2021/3/11--9:52
     * @param array order 商品信息  int address_id 地址id
     * @return array
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:create_order
     * @effect:创建订单接口
     */
    public function createOrder()
    {
        try {
            return $this->orderModel->createOrder();
        } catch (\Exception $e) {
            return $this->format_error('订单创建失败' . $e->getMessage(), '');
        }
    }

    /**
     * 2021/3/11--9:51
     * @param array order['buy_num'=>"购买数量",'goods_id'=>'商品id','sku_id'=>'sku_id'];
     * @return array
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:create_order_before
     * @effect:订单创建前
     */
    public function create_order_before()
    {
        $order_service = new OrderService;
        $rs = $order_service->createOrderBefore();
        return $rs['status'] ? $this->format($rs['data']) : $this->format_error($rs['msg']);
    }

    /**
     * 2021/3/11--14:21
     * @return array
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:orderList
     * @effect:获取订单列表
     */
    public function orderList()
    {
        try {
            return $this->orderModel->getOrders();
        } catch (\Exception $e) {
            return $this->format_error('获取失败', '');
        }
    }


    /**
     * @param Request $request
     * @return array
     * @order_id = 订单ID
     */
    public function orderInfo(Request $request)
    {
        try {
            return $this->orderModel->getOrderInfoById($request->order_id);
        } catch (\Exception $e) {
            return $this->format_error('获取失败', '');
        }
    }


    /**
     * @param Request $request
     * @order_id  订单id
     * @order_status  订单状态
     */
    public function orderChangeStatus(Request $request)
    {
        try {
            return $this->orderModel->editOrderStatus($request->order_id, $request->order_status);
        } catch (\Exception $e) {
            return $this->format_error('更改失败:' . $e->getMessage(), '');
        }
    }


    /**
     * 2021/3/12--16:10
     * @param order_id  订单id  address_id  地址id
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:OrderChangeAddress
     * @effect:修改地址
     */
    public function OrderChangeAddress(Request $request)
    {
        try {
            return $this->orderModel->OrderChangeAddress($request->order_id);
        } catch (\Exception $e) {
            return $this->format_error('更改失败:' . $e->getMessage(), '');
        }
    }

    /**
     * 2021/3/12--19:19
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:delete
     * @effect:订单删除
     */

    public function delete()
    {
        try {
            return $this->orderModel->delete();
        } catch (\Exception $e) {
            return $this->format_error('删除失败' . $e->getMessage());
        }
    }

    /**
     * 2021/3/10--15:20
     * @return \Yansongda\Supports\Collection
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:Pay
     * @effect:订单支付
     */
    public function Pay(orderPay $request)
    {

        $data = $request->validated();
        $order_service = new OrderService();
        return $order_service->payOrder()['data'];

    }
}
